package cn.itsource.org.controller;

import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.PageList;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.query.EmployeeQuery;
import cn.itsource.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 以后叫写接口
 * 部门接口   restFull风格的接口
 * 以后写接口需要考虑
 *   1.请求地址是什么  URL
 *   2.做什么操作
 *   3.返回什么值
 */
@RestController
@RequestMapping("/emp")
@Api(tags = "员工接口",description = "员工接口详细描述")
public class EmployeeController {
    @Autowired
    IEmployeeService employeeService;

    // 查询所有  @GetMapping
    @GetMapping//http://127.0.0.1:8080/dept/
    public List<Employee> queryAll(){
        return employeeService.queryAll();
    }

    // 分页查询
    @PostMapping
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery query){
        return employeeService.queryPage(query);
    }

    // 删除
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除部门",notes = "需要传递ID")
    public AjaxResult deleteById(@PathVariable("id") Long id){
        try {
            employeeService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setMessage("操作失败！"+e.getMessage());
        }
    }

    // 批量删除
    @PatchMapping
    public AjaxResult deleteByIds(@RequestBody Long[] ids) {
        try {
            employeeService.deleteByIds(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setMessage("操作失败！"+e.getMessage());
        }
    }

    // 新增和修改
    @PutMapping
    @ApiOperation(value = "人员添加或修改",notes = "如果有id就是修改否则就是添加")
    public AjaxResult addOrUpdate(@RequestBody Employee employee) {
        try {
            if (employee.getId() != null) {
                employeeService.update(employee);
            }else{
                employeeService.add(employee);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setMessage("操作失败！"+e.getMessage());
        }
    }




}
